#include "devicecombobox.h"

#include <QFileDialog>

#include <dirent.h>
#include <cstdio>
#include <cstring>

using namespace std;
DeviceComboBox::DeviceComboBox(QWidget *parent) :
    QComboBox(parent)
{
    refresh();
}

void DeviceComboBox::refresh(){
    DIR* dirp;
    struct dirent* direntp;
    dirp = opendir("/dev");
    this->clear();
    this->addItem(" ");
    if(dirp != NULL){
        for(direntp = readdir(dirp); direntp != NULL; direntp = readdir(dirp)){
            if(strncmp(direntp->d_name, "video", 5) == 0){
                this->addItem(direntp->d_name);
            }
        }
    }
    this->addItem(FROM_FILE);
}

void DeviceComboBox::selected(QString name){
    QString file;
    if (name == FROM_FILE){
        emit pauseGrab();
        file = QFileDialog::getOpenFileName();
        if(file.isEmpty()){
            emit continueGrab();
            return;
        }
    }else{
        file = name;
    }
    emit changeVideoSource(file);

}
